package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.di.component.DaggerNoticeRentSeekingComponent;
import com.kmproject.haifu.mvp.contract.NoticeRentSeekingContract;
import com.kmproject.haifu.mvp.model.entity.beans.RentSeekingNoticeBean;
import com.kmproject.haifu.mvp.model.entity.resbean.RentSeekingNoticeParmar;
import com.kmproject.haifu.mvp.presenter.NoticeRentSeekingPresenter;
import com.kmproject.haifu.mvp.ui.adapter.NoticeRentSeekingAdapter;
import com.kmproject.haifu.mvp.ui.widget.pullRefresh.PullToRefreshView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 09/05/2019 11:49
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class NoticeRentSeekingActivity extends BaseActivity<NoticeRentSeekingPresenter> implements NoticeRentSeekingContract.View {

    @BindView(R.id.toolbar_backImg)
    ImageView toolbarBackImg;
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.NoticeRentSeeking_RecyclerView)
    RecyclerView NoticeRentSeekingRecyclerView;
    @BindView(R.id.NoticeRentSeeking_PullToRefreshView)
    PullToRefreshView NoticeRentSeekingPullToRefreshView;
    NoticeRentSeekingAdapter adapter;
    RentSeekingNoticeParmar rentSeekingNoticeParmar=new RentSeekingNoticeParmar();
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerNoticeRentSeekingComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_notice_rent_seeking; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        toolbarTitle.setTextColor(ArmsUtils.getColor(this, R.color.white));
        toolbarBackImg.setImageResource(R.drawable.member_center_main_back_img);
        toolbar.setBackgroundColor(ArmsUtils.getColor(this, R.color.color_member_bg));

        NoticeRentSeekingPullToRefreshView.setEnableRefresh(false);
        NoticeRentSeekingRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter=new NoticeRentSeekingAdapter(new ArrayList<>());
        adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        NoticeRentSeekingRecyclerView.setAdapter(adapter);
        NoticeRentSeekingPullToRefreshView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                rentSeekingNoticeParmar.setPage(rentSeekingNoticeParmar.getPage()+1);
             mPresenter.rentSeekingNotice(rentSeekingNoticeParmar);
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter madapter, View view, int position) {
                Intent intent=new Intent(NoticeRentSeekingActivity.this,RentSeekingDetailActivity.class);
                intent.putExtra("seekingId",adapter.getItem(position).getRentSeekingId());
                ArmsUtils.startActivity(intent);

            }
        });
        mPresenter.rentSeekingNotice(rentSeekingNoticeParmar);
    }

    @Override
    public void showLoading() {
       loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void rentSeekingNoticeSuccess(int curryPage,int totalPage,List<RentSeekingNoticeBean> rentSeekingNoticeBeans) {
        NoticeRentSeekingPullToRefreshView.finishLoadMore();
        if (rentSeekingNoticeBeans == null || rentSeekingNoticeBeans.isEmpty()) {
            NoticeRentSeekingPullToRefreshView.setEnableLoadMore(false);
            if (curryPage == 1) {
                adapter.setEmptyView(AppUtil.setEmptyView(NoticeRentSeekingActivity.this, "没有求租通知信息", "", null, true));
            }
            return;
        }
        rentSeekingNoticeParmar.setPage(curryPage);
        if (curryPage >= totalPage) {
            NoticeRentSeekingPullToRefreshView.setEnableLoadMore(false);
        } else {
            NoticeRentSeekingPullToRefreshView.setEnableLoadMore(true);
        }
        if (adapter.getData() == null || adapter.getData().isEmpty()) {
            adapter.setNewData(rentSeekingNoticeBeans);
        } else {
            adapter.addData(rentSeekingNoticeBeans);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
